import {
	createRouter,
	createWebHistory
} from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

// 导入布局组件
import Layout from '@/views/layout/index.vue'

// 路由配置
const routes = [
	{
		path: '/login',
		name: 'Login',
		component: () => import('@/views/login/index.vue'),
		meta: {
			title: '登录'
		}
	},
	{
		path: '/forgot-password',
		name: 'ForgotPassword',
		component: () => import('@/views/forgot-password/index.vue'),
		meta: {
			title: '忘记密码'
		}
	},
	{
		path: '/',
		redirect: '/dashboard'
	},
	{
		path: '/dashboard',
		component: Layout,
		meta: {
			title: '首页'
		},
		children: [{
			path: '',
			name: 'Dashboard',
			component: () => import('@/views/dashboard/index.vue'),
			meta: {
				title: '首页'
			}
		}]
	},
	// 基础信息
	{
		path: '/base-info',
		component: Layout,
		redirect: '/base-info/supplier',
		meta: {
			title: '基础信息'
		},
		children: [{
				path: 'supplier',
				name: 'SupplierManagement',
				component: () => import('@/views/base-info/supplier/index.vue'),
				meta: {
					title: '供应商管理'
				}
			},
			{
				path: 'customer',
				name: 'CustomerManagement',
				component: () => import('@/views/base-info/customer/index.vue'),
				meta: {
					title: '客户管理'
				}
			},
			{
				path: 'owner',
				name: 'OwnerManagement',
				component: () => import('@/views/base-info/owner/index.vue'),
				meta: {
					title: '货主管理'
				}
			},
			{
				path: 'warehouse',
				name: 'WarehouseManagement',
				component: () => import('@/views/base-info/warehouse/index.vue'),
				meta: {
					title: '仓库管理'
				}
			},
			{
				path: 'warehouse-area',
				name: 'AreaManagement',
				component: () => import('@/views/base-info/warehouse-area/index.vue'),
				meta: {
					title: '库区管理'
				}
			},
			{
				path: 'storage-shelf',
				name: 'ShelfManagement',
				component: () => import('@/views/base-info/storage-shelf/index.vue'),
				meta: {
					title: '货架管理'
				}
			},
			{
				path: 'storage-location',
				name: 'LocationManagement',
				component: () => import('@/views/base-info/storage-location/index.vue'),
				meta: {
					title: '货位管理'
				}
			}
		]
	},
	// 商品
	{
		path: '/commodity',
		component: Layout,
		redirect: '/commodity/info',
		meta: {
			title: '商品'
		},
		children: [{
				path: 'info',
				name: 'commodityInfo',
				component: () => import('@/views/commodity/commodity-info/index.vue'),
				meta: {
					title: '商品信息'
				}
			},
			{
				path: 'detail',
				name: 'commodityDetail',
				component: () => import('@/views/commodity/commodity-info/commodity-detail.vue'),
				meta: {
					title: '商品详情'
				}
			},
			{
				path: 'property',
				name: 'commodityProperty',
				component: () => import('@/views/commodity/commodity-property/index.vue'),
				meta: {
					title: '商品属性'
				}
			}
		]
	},
	// 用户权限
	{
		path: '/user-permission',
		component: Layout,
		redirect: '/user-permission/user',
		meta: {
			title: '公司信息'
		},
		children: [
			{
				path: 'role',
				name: 'RolePermissionManagement',
				component: () => import('@/views/user-permission/role/index.vue'),
				meta: {
					title: '角色权限管理'
				}
			},
			{
				path: 'organization',
				name: 'OrganizationManagement',
				component: () => import('@/views/user-permission/organization/index.vue'),
				meta: {
					title: '公司信息'
				}
			},
			{
				path: 'employee-role',
				name: 'EmployeeRoleManagement',
				component: () => import('@/views/user-permission/employee-role/index.vue'),
				meta: {
					title: '员工角色管理'
				}
			}
		]
	},

	// 入库模块
	{
		path: '/inbound',
		component: Layout,
		redirect: '/inbound/pending-operation',
		meta: {
			title: '入库模块'
		},
		children: [{
				path: 'pending-operation',
				name: 'PendingOperationManagement',
				component: () => import('@/views/inbound/pending-operation/index.vue'),
				meta: {
					title: '入库待作业管理'
				}
			},
			{
				path: 'receiving',
				name: 'ReceivingManagement',
				component: () => import('@/views/inbound/receiving/index.vue'),
				meta: {
					title: '收货入库管理'
				}
			},
			{
				path: 'putaway',
				name: 'PutawayManagement',
				component: () => import('@/views/inbound/putaway/index.vue'),
				meta: {
					title: '上架管理'
				}
			}
		]
	},

	// 出库模块
	{
		path: '/outbound',
		component: Layout,
		redirect: '/outbound/pending-allocation',
		meta: {
			title: '出库模块'
		},
		children: [{
				path: 'pending-allocation',
				name: 'PendingAllocationManagement',
				component: () => import('@/views/outbound/pending-allocation/index.vue'),
				meta: {
					title: '出库待分配'
				}
			},
			{
				path: 'order-picking',
				name: 'OrderPickingManagement',
				component: () => import('@/views/outbound/order-picking/index.vue'),
				meta: {
					title: '按单配货管理'
				}
			},
			{
				path: 'check',
				name: 'CheckManagement',
				component: () => import('@/views/outbound/check/index.vue'),
				meta: {
					title: '复核验货管理'
				}
			},
			{
				path: 'shortage-order',
				name: 'ShortageOrderManagement',
				component: () => import('@/views/outbound/shortage-order/index.vue'),
				meta: {
					title: '缺货订单管理'
				}
			},
			{
				path: 'pending-outbound',
				name: 'PendingOutboundManagement',
				component: () => import('@/views/outbound/pending-outbound/index.vue'),
				meta: {
					title: '待出库管理'
				}
			},
			{
				path: 'order-query',
				name: 'OrderQueryManagement',
				component: () => import('@/views/outbound/order-query/index.vue'),
				meta: {
					title: '订单查询管理'
				}
			},
			{
				path: 'inventory-count',
				name: 'InventoryCountManagement',
				component: () => import('@/views/outbound/inventory-count/index.vue'),
				meta: {
					title: '盘点功能管理'
				}
			}
		]
	},

	// 库内模块
	{
		path: '/inventory',
		component: Layout,
		redirect: '/inventory/discrepancy',
		meta: {
			title: '库内模块'
		},
		children: [{
				path: 'discrepancy',
				name: 'DiscrepancyManagement',
				component: () => import('@/views/inventory/discrepancy/index.vue'),
				meta: {
					title: '差异管理'
				}
			},
			{
				path: 'transfer',
				name: 'TransferManagement',
				component: () => import('@/views/inventory/transfer/index.vue'),
				meta: {
					title: '移库功能管理'
				}
			},
			{
				path: 'conversion',
				name: 'ConversionManagement',
				component: () => import('@/views/inventory/conversion/index.vue'),
				meta: {
					title: '正次转换管理'
				}
			},
			{
				path: 'inventory-query',
				name: 'InventoryQueryManagement',
				component: () => import('@/views/inventory/inventory-query/index.vue'),
				meta: {
					title: '库存查询管理'
				}
			},
			{
				path: 'location-query',
				name: 'LocationQueryManagement',
				component: () => import('@/views/inventory/location-query/index.vue'),
				meta: {
					title: '货位查询管理'
				}
			}
		]
	},

	// 配置模块
	{
		path: '/configuration',
		component: Layout,
		redirect: '/configuration/repo-config',
		meta: {
			title: '配置模块'
		},
		children: [{
				path: 'repo-config',
				name: 'RepoConfig',
				component: () => import('@/views/configuration/repo-config/index.vue'),
				meta: {
					title: '仓库配置'
				}
			},
			{
			  path: 'print-management',
			  name: 'PrintManagement',
			  component: () => import('@/views/configuration/print-management/index.vue'),
			  meta: { title: '打印管理' }
			},
			{
			  path: 'print-management/designer',
			  name: 'ExpressTemplateDesigner',
			  component: () => import('@/views/configuration/print-management/designer/index.vue'),
			  meta: { title: '面单设计器', activeMenu: '/configuration/print-management' }
			},
			{
			  path: 'print-management/preview',
			  name: 'PrintTemplatePreview',
			  component: () => import('@/views/configuration/print-management/preview/index.vue'),
			  meta: { title: '模板预览', activeMenu: '/configuration/print-management' }
			},
			{
			  path: 'print-management/preview/print-template',
			  name: 'printTemplate',
			  component: () => import('@/views/configuration/print-management/preview/print-template/index.vue'),
			  meta: { title: '打印示例', activeMenu: '/configuration/print-management' }
			}
		]
	},
	// 用户信息
	{
		path: '/user',
		component: Layout,
		meta: {
			title: '用户信息'
		},
		children: [{
			path: 'profile',
			name: 'UserProfile',
			component: () => import('@/views/user/profile/index.vue'),
			meta: {
				title: '个人信息'
			}
		}]
	},
	// 404页面
	{
		path: '/:pathMatch(.*)*',
		name: 'NotFound',
		component: () => import('@/views/error/404.vue'),
		meta: {
			title: '404'
		}
	},
	// 重定向页面
	{
		path: '/redirect/:path*',
		component: () => import('@/views/redirect/index.vue'),
		hidden: true
	}
]

const router = createRouter({
	history: createWebHistory(),
	routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
	NProgress.start()
	document.title = to.meta.title ? `${to.meta.title} - WMS系统` : 'WMS系统'

	const token = localStorage.getItem('access_token')

	// 无需登录即可访问的白名单路径
	const whiteList = ['/login', '/forgot-password']

	if (whiteList.includes(to.path)) {
		next()
	} else {
		if (token) {
			next()
		} else {
			next('/login')
		}
	}
})

router.afterEach(() => {
	NProgress.done()
})

export default router